package com.jlect.swebing.ui.laf.basic.client;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Widget;
import com.jlect.swebing.ui.client.Dimension;
import com.jlect.swebing.ui.client.Image;
import com.jlect.swebing.ui.client.PropertyChangeEvent;
import com.jlect.swebing.ui.client.button.GButton;
import com.jlect.swebing.ui.client.laf.ButtonUI;
import com.jlect.swebing.ui.client.laf.ComponentUI;

/**
 * Label's UI part implementation
 *
 * @author Sergey Kozmin
 * @since 04.12.2007 10:29:56
 */
public class BasicButtonUI extends AbstractComponentUI implements ButtonUI {
    public ComponentUI createInstance() {
        return new BasicButtonUI();
    }

    protected GButton getComponent() {
        return (GButton) super.getComponent();
    }

    private Button getButtonWidget() {
        return (Button) ((CompositeAttachEventProvider)getWidget()).getTargetWidget();
    }

    public Widget provideWidget() {
        String text = getComponent().getText();
        Image image = getComponent().getImage();
        String btnHtml = getHtml(text, image);
        Button button = new Button();
        button.setHTML(btnHtml);
//        button.addClickListener(this);
        return new CompositeAttachEventProvider(button);
    }

    public Dimension getPreferredSize() {
        GButton button = getComponent();
        FontMetrics metrics = FontMetrics.createFontMetric(button);
        int height = metrics.getHeight(button.getText());
        int width = metrics.getWidth(button.getText());
        return new Dimension(width, height);  //@todo optimize
    }

    public Dimension getMaximumSize() {
        return getPreferredSize();  //@todo change to max size
    }

    public Dimension getMinimumSize() {
        return getPreferredSize();
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if(GButton.BUTTON_TEXT_PROPERTY_NAME.equalsIgnoreCase(evt.getPropertyName()) ||
                GButton.BUTTON_TEXT_PROPERTY_NAME.equalsIgnoreCase(evt.getPropertyName())) {//todo optimize
            String text = getComponent().getText();
            Image image = getComponent().getImage();
            String btnHtml = getHtml(text, image);
            getButtonWidget().setHTML(btnHtml);
        } else {
            super.propertyChange(evt);
        }
    }

    private String getHtml(String text, Image image) {//todo enhance
        String btnHtml;
        if (text != null) {
            btnHtml = text;
        } else if (image != null) {
            btnHtml = getImageHtml(image.getUrl());
        } else {
            throw new IllegalArgumentException("Either text or icon objects should be not null");
        }
        return btnHtml;
    }

    public void setEnabled(boolean enabled) {
        getButtonWidget().setEnabled(enabled);
    }

    private static String getImageHtml(String url) {
        return "<img src=" + url + "/>";
    }

    /* public void setText(String text) {
        this.text = text;
    }

    public void setIcon(Image image) {
        this.image = image;
    }

    public void setActionListener(RendererActionListener listener) {
        clickListener = listener;
    }

    private Button getButton() {
        return (Button) ((CompositeAttachEventProvider)getWidget()).getTargetWidget();
    }*/

    /*public void onClick(Widget sender) {
        if(clickListener != null) {
            if(event == null) {
                event = new ClickActionEvent(this, 0);//todo implement modifiers
            }
            clickListener.fireActionPerformed(event);
        }
    }

    public void setEnabled(boolean enabled) {
        getButton().setEnabled(enabled);
    }*/
}